
/**
* 上海中赢金融信息服务有限公司
* Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
*/
package com.chinazyjr.framework.web.monitor.manager.logic.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chinazyjr.framework.common.model.sdto.APIRequest;
import com.chinazyjr.framework.common.monitor.Monitor_account;
import com.chinazyjr.framework.common.monitor.Monitor_activity;
import com.chinazyjr.framework.common.monitor.Monitor_bank;
import com.chinazyjr.framework.common.monitor.Monitor_openlab;
import com.chinazyjr.framework.common.monitor.Monitor_order;
import com.chinazyjr.framework.common.monitor.Monitor_product_from;
import com.chinazyjr.framework.common.monitor.Monitor_product_plan;
import com.chinazyjr.framework.common.monitor.Monitor_product_sale;
import com.chinazyjr.framework.common.monitor.Monitor_public;
import com.chinazyjr.framework.common.monitor.Monitor_user;
import com.chinazyjr.framework.common.zookeeper.ZKNameSpaceEnum;
import com.chinazyjr.framework.common.zookeeper.ZookeeperFacade;
import com.chinazyjr.framework.web.monitor.enums.ProviderEnum;
import com.chinazyjr.framework.web.monitor.enums.ProviderStatusEnum;
import com.chinazyjr.framework.web.monitor.manager.logic.MonitorLogic;
import com.chinazyjr.framework.web.monitor.manager.logic.MonitorZKNode;
import com.chinazyjr.framework.web.monitor.model.dto.ServiceProvider;
import com.sys.midware.assist.utils.string.StringUtil;
import com.sys.midware.zookeeper.curator.ZKRegistryCenter;

/**
 * <b>Description：</b> 监控逻辑 <br/>
 * <b>ClassName：</b> MonitorLogic <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月25日 上午10:39:05 <br/>
 * <b>@version: </b> <br/>
 */
@Component("monitorLogic")
public class MonitorLogicImpl implements MonitorLogic {

    private static final Logger logger = LoggerFactory.getLogger(MonitorLogicImpl.class);

    private ZKRegistryCenter getZKClient() {
        return ZookeeperFacade.getZKClient(ZKNameSpaceEnum.MONITOR_API.getValue());
    }

    @Override
    public List<ServiceProvider> getList() {
        ZKRegistryCenter zk = getZKClient();
        List<String> providerNameList = null;

        if (!zk.isExisted(MonitorZKNode.PATH_ROOT)) {
            zk.persist(MonitorZKNode.PATH_ROOT);
        }

        providerNameList = zk.getChildrenKeys(MonitorZKNode.PATH_ROOT);
        List<ServiceProvider> list = new ArrayList<ServiceProvider>();
        if (providerNameList != null && !providerNameList.isEmpty()) {
            MonitorZKNode node = new MonitorZKNode();

            for (String providerName : providerNameList) {
                ServiceProvider provider = new ServiceProvider();
                provider.setProviderName(providerName);
                provider.setProviderDesc(zk.getData(node.getProviderDescPath(providerName)));
                provider.setStatus(zk.getData(node.getProviderStatusPath(providerName)));
                list.add(provider);
            }

        } else {// 初始化
            List<Map<String, Object>> providerList = ProviderEnum.toList();
            for (Map<String, Object> map : providerList) {
                ServiceProvider pro = new ServiceProvider();
                pro.setProviderName((String) map.get("value"));
                pro.setProviderDesc((String) map.get("desc"));
                list.add(pro);
                save(pro);
            }
        }

        return list;
    }

    @Override
    public void save(ServiceProvider serviceProvider) {
        ZKRegistryCenter zk = getZKClient();
        MonitorZKNode node = new MonitorZKNode();
        zk.persist(node.getProviderNamePath(serviceProvider.getProviderName()), serviceProvider.getProviderName());
        zk.persist(node.getProviderDescPath(serviceProvider.getProviderName()), serviceProvider.getProviderDesc());
        zk.persist(node.getProviderStatusPath(serviceProvider.getProviderName()), ProviderStatusEnum.NONE.getValue());

    }

    @Override
    public void delete(String providerName) {
        ZKRegistryCenter zk = getZKClient();
        MonitorZKNode node = new MonitorZKNode();
        if (zk.isExisted(node.getProviderRootPath(providerName))) {
            zk.remove(node.getProviderRootPath(providerName));
        }
    }

    @Override
    public void update(ServiceProvider serviceProvider) {
        ZKRegistryCenter zk = getZKClient();
        MonitorZKNode node = new MonitorZKNode();
        if (StringUtil.isNotEmpty(serviceProvider.getProviderName())) {
            zk.update(node.getProviderNamePath(serviceProvider.getProviderName()), serviceProvider.getProviderName());
        }
        if (StringUtil.isNotEmpty(serviceProvider.getProviderDesc())) {
            zk.update(node.getProviderDescPath(serviceProvider.getProviderName()), serviceProvider.getProviderDesc());
        }
        if (StringUtil.isNotEmpty(serviceProvider.getStatus())) {
            zk.update(node.getProviderStatusPath(serviceProvider.getProviderName()), serviceProvider.getStatus());
        }

        // zk.update(node.getProviderStatusPath(serviceProvider.getProviderName()),
        // ProviderStatusEnum.NONE.getValue());
    }

    @Override
    public ServiceProvider get(String providerName) {
        ZKRegistryCenter zk = getZKClient();
        ServiceProvider provider = new ServiceProvider();
        MonitorZKNode node = new MonitorZKNode();
        provider.setProviderName(zk.getData(node.getProviderNamePath(providerName)));
        provider.setProviderDesc(zk.getData(node.getProviderDescPath(providerName)));
        provider.setStatus(zk.getData(node.getProviderStatusPath(providerName)));
        // 连接provider
        connect(provider);

        return provider;
    }

    @Autowired
    private Monitor_account monitor_account;

    @Autowired
    private Monitor_activity monitor_activity;

    @Autowired
    private Monitor_bank monitor_bank;

    @Autowired
    private Monitor_openlab monitor_openlab;

    @Autowired
    private Monitor_order monitor_order;

    @Autowired
    private Monitor_product_from monitor_product_from;

    @Autowired
    private Monitor_product_plan monitor_product_plan;

    @Autowired
    private Monitor_product_sale monitor_product_sale;

    @Autowired
    private Monitor_public monitor_public;

    @Autowired
    private Monitor_user monitor_user;

    @Override
    public List<ServiceProvider> connect() {

        List<ServiceProvider> providerList = getList();

        if (providerList != null && !providerList.isEmpty()) {

            for (ServiceProvider provider : providerList) {
                connect(provider);
            }
        }
        return providerList;
    }

    private void connect(ServiceProvider provider) {
        APIRequest request = new APIRequest();
        try {
            String prividerName = provider.getProviderName();
            if (ProviderEnum.PUBLIC.getValue().equals(prividerName)) {
                monitor_public.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.ACCOUNT.getValue().equals(prividerName)) {
                monitor_account.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.ACTIVITY.getValue().equals(prividerName)) {
                monitor_activity.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.BANK.getValue().equals(prividerName)) {
                monitor_bank.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.ORDER.getValue().equals(prividerName)) {
                monitor_order.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.OPENLAB.getValue().equals(prividerName)) {
                monitor_openlab.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.PRODUCT_FROM.getValue().equals(prividerName)) {
                monitor_product_from.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.PRODUCT_PLAN.getValue().equals(prividerName)) {
                monitor_product_plan.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.PRODUCT_SALE.getValue().equals(prividerName)) {
                monitor_product_sale.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            } else if (ProviderEnum.USER.getValue().equals(prividerName)) {
                monitor_user.connect(request);
                provider.setStatus(ProviderStatusEnum.SUCESS.getValue());
            }

        } catch (Exception e) {
            logger.error("monitor connect error!" + e.getMessage());
            provider.setStatus(ProviderStatusEnum.FAILURE.getValue());
            provider.setMsg(e.getMessage());
        }

        // return provider;
    }

}
